package com.yc.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.vote.entity.Items;
import com.yc.vote.entity.Record;
import com.yc.vote.entity.Vote;
import com.yc.vote.mapper.ItemsMapper;
import com.yc.vote.mapper.RecordMapper;
import com.yc.vote.mapper.VoteMapper;
import com.yc.vote.service.BizException;
import com.yc.vote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 * 投票系统-题目表-2 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-14
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {

    @Resource
    ItemsMapper itemsMapper;


    @Override
    @Transactional  //开启事务  2张表的操作
    public void create(Vote entity) throws BizException{
        //字段验证
        if ( entity.getVname() == null || entity.getVname().trim().isEmpty()){
            throw new BizException("请输入标题！");
        }

        if ( entity.getVtype() == null || entity.getVtype().trim().isEmpty()){
            throw new BizException("请输入类型！");
        }

        if ( entity.getUsid() == null){
            throw new BizException("请登陆系统！");
        }

        if ( entity.getItems() == null || entity.getItems().size() < 2){
            throw new BizException("至少两个选项！");
        }

        //同名验证
        LambdaQueryWrapper<Vote> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vote::getId,entity.getId());
        queryWrapper.eq(Vote::getVname,entity.getVname());
        long count = count(queryWrapper);
        if (count > 0){
            throw new BizException("您已经发起过该投票！");
        }

        //保存 vote 表记录 save完成之后，mybatis 会返回该记录的主键
        if (save(entity)){
//            System.out.println("entity = " + entity);
            entity.getItems().forEach( item ->{
               item.setVid(entity.getId());  //设置关联外键
               itemsMapper.insert(item);
           });
           return ;
        }

        throw new BizException("新增投票失败！");

    }

    @Resource
    RecordMapper recordMapper;


    @Transactional
    public void dlById(int id) throws BizException{
        //判断是否已经投票
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Record::getVid,id);
        Long count = recordMapper.selectCount(lqw);

        if (count > 0){
            //已有投票：逻辑删除
            Vote vote = new Vote();
            vote.setId(id);
            vote.setStatus(0);
            this.updateById(vote);
        }else{

            //未投票：物理删除
            //先删除item表的记录
            LambdaQueryWrapper<Items> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Items::getVid,id);
            itemsMapper.delete(queryWrapper);

            //再删除vote表的记录
            this.removeById(id);
        }

    }

}
